package com.yenlien.traceyun.breed.service;

import com.yenlien.traceyun.breed.bean.PreventBean;
import com.yenlien.traceyun.breed.dao.BreedDao;
import com.yenlien.traceyun.breed.dao.BreedInputsDao;
import com.yenlien.traceyun.breed.dao.BreedInputsLogDao;
import com.yenlien.traceyun.breed.dao.PreventDao;
import com.yenlien.traceyun.breed.entity.Breed;
import com.yenlien.traceyun.breed.entity.BreedInputs;
import com.yenlien.traceyun.breed.entity.BreedInputsLog;
import com.yenlien.traceyun.breed.entity.Prevent;
import com.yenlien.traceyun.business.interceptor.UserAuthUtil;
import com.yenlien.traceyun.common.utils.Tools;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.persistence.criteria.*;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by LiuSha on 2020/9/24.
 */
@Service
public class PreventService {
    @Autowired
    private PreventDao preventDao;
    @Autowired
    private BreedDao breedDao;
    @Autowired
    private BreedInputsDao breedInputsDao;
    @Autowired
    private BreedInputsLogDao breedInputsLogDao;

    public Page<Prevent> findByList(final Prevent prevent) {
        Pageable pageable = PageRequest.of(prevent.getPage(), prevent.getPageSize(), Sort.Direction.DESC, "createtime");
        return preventDao.findAll(new Specification<Prevent>() {
            private static final long serialVersionUID = 1L;

            @Override
            public Predicate toPredicate(Root<Prevent> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                //构造查询条件
                List<Predicate> list_and = new ArrayList<>();
                List<Predicate> list_or = new ArrayList<>();
                if (!StringUtils.isEmpty(prevent.getCompanyId())) {
                    list_and.add(cb.equal(root.get("companyId"), prevent.getCompanyId()));
                }
                if (!StringUtils.isEmpty(prevent.getBreedId())) {
                    list_and.add(cb.equal(root.get("breedId"), prevent.getBreedId()));
                }
                Predicate[] predicate = new Predicate[list_and.size()];
                Predicate pre_and = cb.and(list_and.toArray(predicate));
                if (!StringUtils.isEmpty(prevent.getQuery())) {
                    list_or.add(cb.like(root.get("code"), "%" + prevent.getQuery() + "%"));
                    list_or.add(cb.like(root.get("inputsName"), "%" + prevent.getQuery() + "%"));
                    list_or.add(cb.like(root.get("preventBatch"), "%" + prevent.getQuery() + "%"));
                    list_or.add(cb.like(root.get("preventName"), "%" + prevent.getQuery() + "%"));
                } else {
                    return pre_and;
                }
                Predicate[] or = new Predicate[list_or.size()];
                Predicate pre_or = cb.or(list_or.toArray(or));
                return query.where(pre_and, pre_or).getRestriction();
            }
        }, pageable);
    }

    @Transactional
    public void save(PreventBean preventBean) {
        String companyId = UserAuthUtil.getLoginUser().getCompanyId();
        if (preventBean.getPrevents().size() > 0) {
            for (Prevent prevent : preventBean.getPrevents()) {
                prevent.setCompanyId(companyId);
                prevent.setBreedId(preventBean.getBreedId());
                prevent.setBatch(preventBean.getBatch());
                prevent.setSeedName(preventBean.getSeedName());
                prevent.setInputsId(preventBean.getInputsId());
                prevent.setInputsName(preventBean.getInputsName());
                prevent.setInputsBatch(preventBean.getInputsBatch());
                prevent.setPreventName(preventBean.getPreventName());
                prevent.setPreventTime(preventBean.getPreventTime());
                prevent.setPreventBatch(preventBean.getPreventBatch());
                prevent.setLastStep(prevent.getBreedId());
                preventDao.save(prevent);
            }
            BreedInputs breedInputs = breedInputsDao.findById(preventBean.getInputsId());
            if (!StringUtils.isEmpty(breedInputs.getExpend())) {
                breedInputs.setExpend(Tools.addtr(breedInputs.getExpend(), preventBean.getInputsNumber()));
            } else {
                breedInputs.setExpend(Tools.addtr("0", preventBean.getInputsNumber()));
            }
            breedInputsDao.save(breedInputs);
            BreedInputsLog breedInputsLog = new BreedInputsLog();
            breedInputsLog.setInputsId(preventBean.getInputsId());
            breedInputsLog.setNum(preventBean.getInputsNumber());
            breedInputsLog.setTime(Tools.getCurrentTime());
            breedInputsLog.setUserName(preventBean.getPreventName());
            breedInputsLogDao.save(breedInputsLog);
        }
    }

    public Breed findById(String id) {
        Breed breed = breedDao.findById(id);
        breed.setPrevent(preventDao.findByBreedId(id).size());
        return breed;
    }

    public void delete(String id) {
        preventDao.deleteById(id);
    }
}
